using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiBuilder.Services;
using System.Reflection;

namespace MauiBuilder.ViewModels
{
    /// <summary>
    /// 关于我们页面的视图模型
    /// 显示应用信息、版本、开发团队等信息
    /// </summary>
    public partial class AboutViewModel : BaseViewModel
    {
        private readonly LogManager _logManager = LogManager.Instance;

        public AboutViewModel()
        {
            _logManager.LogInfo("AboutViewModel 初始化");
        }

        #region 计算属性

        /// <summary>
        /// 应用名称
        /// </summary>
        public string AppName => "MAUI 智能应用";

        /// <summary>
        /// 应用版本
        /// </summary>
        public string AppVersion => "1.0.0";

        /// <summary>
        /// 构建版本
        /// </summary>
        public string BuildVersion => "Build 2024.10.07";

        /// <summary>
        /// 公司名称
        /// </summary>
        public string CompanyName => "我的科技公司";

        /// <summary>
        /// 版权信息
        /// </summary>
        public string Copyright => $"© 2024 {CompanyName}. All rights reserved.";

        /// <summary>
        /// 应用描述
        /// </summary>
        public string AppDescription => "这是一个基于 .NET MAUI 构建的现代化跨平台移动应用，" +
            "集成了前台服务、定时通知、用户管理等功能，为用户提供优质的移动体验。";

        /// <summary>
        /// 技术栈信息
        /// </summary>
        public List<TechStackItem> TechStack => new()
        {
            new("📱", ".NET MAUI", "跨平台UI框架"),
            new("🔧", "CommunityToolkit.Mvvm", "MVVM模式支持"),
            new("🎨", "Material Design", "现代化UI设计"),
            new("🔔", "前台服务", "后台通知功能"),
            new("📊", "日志系统", "完整的日志记录"),
            new("🔐", "权限管理", "Android权限处理")
        };

        /// <summary>
        /// 开发团队信息
        /// </summary>
        public List<TeamMember> DevelopmentTeam => new()
        {
            new("👨‍💻", "张三", "高级开发工程师", "负责核心功能开发"),
            new("👩‍🎨", "李四", "UI/UX设计师", "负责界面设计与用户体验"),
            new("👨‍💼", "王五", "产品经理", "负责产品规划与需求分析"),
            new("🧪", "赵六", "测试工程师", "负责质量保证与测试")
        };

        /// <summary>
        /// 联系方式
        /// </summary>
        public List<ContactItem> ContactInfo => new()
        {
            new("📧", "邮箱", "support@mycompany.com"),
            new("🌐", "官网", "www.mycompany.com"),
            new("📱", "客服热线", "400-123-4567"),
            new("💬", "微信公众号", "MyCompanyOfficial")
        };

        #endregion

        #region 命令

        /// <summary>
        /// 检查更新命令
        /// </summary>
        [RelayCommand]
        private async Task OnCheckForUpdates()
        {
            try
            {
                _logManager.LogInfo("用户检查应用更新");
                
                // 模拟检查更新
                await Task.Delay(2000);
                
                await AppShell.DisplayAlertAsync(
                    "检查更新", 
                    "🎉 您正在使用最新版本！", 
                    "确定");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"检查更新失败: {ex.Message}", "确定");
            }
        }

        /// <summary>
        /// 查看许可证命令
        /// </summary>
        [RelayCommand]
        private async Task OnViewLicense()
        {
            string licenseText = "MIT License\n\n" +
                "Copyright (c) 2024 我的科技公司\n\n" +
                "Permission is hereby granted, free of charge, to any person obtaining a copy " +
                "of this software and associated documentation files...";
                
            await AppShell.DisplayAlertAsync(
                "开源许可证", 
                licenseText, 
                "确定");
                
            _logManager.LogInfo("用户查看了许可证信息");
        }

        /// <summary>
        /// 发送反馈命令
        /// </summary>
        [RelayCommand]
        private async Task OnSendFeedback()
        {
            string result = await AppShell.Current.DisplayPromptAsync(
                "用户反馈",
                "请输入您的宝贵意见和建议：",
                "发送",
                "取消",
                "请输入反馈内容...",
                maxLength: 500);
                
            if (!string.IsNullOrWhiteSpace(result))
            {
                _logManager.LogInfo($"用户提交反馈: {result}");
                
                await AppShell.DisplayAlertAsync(
                    "反馈已提交", 
                    "感谢您的反馈！我们会认真考虑您的建议。", 
                    "确定");
            }
        }

        /// <summary>
        /// 分享应用命令
        /// </summary>
        [RelayCommand]
        private async Task OnShareApp()
        {
            try
            {
                await Share.RequestAsync(new ShareTextRequest
                {
                    Text = $"推荐一款优秀的移动应用：{AppName}！功能强大，界面美观，值得下载体验！",
                    Title = "分享应用"
                });
                
                _logManager.LogInfo("用户分享了应用");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"分享失败: {ex.Message}", "确定");
            }
        }

        #endregion
    }

    /// <summary>
    /// 技术栈项目
    /// </summary>
    public record TechStackItem(string Icon, string Name, string Description);

    /// <summary>
    /// 团队成员
    /// </summary>
    public record TeamMember(string Icon, string Name, string Role, string Description);

    /// <summary>
    /// 联系方式项目
    /// </summary>
    public record ContactItem(string Icon, string Type, string Value);
}